****-- Code explaining how to build the datasets used in "Main_rep_JEL" and "Appendix_rep_JEL"

* Create a folder called "rep_data". Use it to store the original datasets downladed per instructions in README.txt
* Add the path to this folder 

global pathData = ".../rep_data" 
cd "$pathData"


********************************************************************************
********************************************************************************
************ ESS_selected_waves.dta

*-- round 1 (2002) -> ESS1e06_6.dta 

use "ESS1e06_6.dta", replace

gen year = 2002 
gen cntryOK = 1 if inlist(cntry, "BE", "GB", "DE", "FI", "AT")
replace cntryOK = 1 if inlist(cntry, "FR", "PT", "SE", "NL", "DK")
keep if cntryOK == 1

keep imbleco pspw year cntry

save "ESS2002.dta", replace


*-- round 4 (2008) -> ESS4e04_2.dta

use "ESS4e04_2.dta", replace

gen year = 2008
gen cntryOK = 1 if inlist(cntry, "GB", "FR", "DE")
keep if cntryOK == 1
keep imsclbn imrccon pspw year cntry ///
schtaut hrshsnt trrprsn ///
uentrjb bennent prtsick  sblazy sblwcoa sblwlka ///
imbgeco imueclt imwbcnt imdfetn impcntr imsmetn 

save "ESS2008.dta", replace


*-- round 7 (2014) -> ESS7e02_2.dta

use "ESS7e02_2.dta", replace

gen year = 2014
gen cntryOK = 1 if inlist(cntry, "BE", "GB", "DE", "FI", "AT")
replace cntryOK = 1 if inlist(cntry, "FR", "PT", "SE", "NL", "DK")
keep if cntryOK == 1
keep imbleco gvtrimg pspw year cntry

save "ESS2014.dta", replace


*-- round 8 (2016) -> ESS8e02_1.dta

use "ESS8e02_1.dta", replace

gen year = 2016
gen cntryOK = 1 if inlist(cntry, "BE", "GB", "DE", "FI", "AT")
replace cntryOK = 1 if inlist(cntry, "FR", "PT", "SE", "DK", "NL")
keep if cntryOK == 1
keep imbgeco imueclt imwbcnt imdfetn impcntr imsmetn gincdif gvslvue pspw year cntry imsclbn 

save "ESS2016.dta", replace


use "ESS2002.dta", replace
append using "ESS2008.dta"
append using "ESS2014.dta"
append using "ESS2016.dta"


save "ESS_selected_waves", replace 



********************************************************************************
********************************************************************************
********************************************************************************
************ BES_panelW2.dta

*-- latest version of the original data file (on 08/09/2022): BES2019_W21_Panel_v21.0
clear all
set maxvar 10000

use "BES2019_W21_Panel_v21.0.dta", replace

keep if wave2 == 1


save "BES_panelW2", replace 


********************************************************************************
********************************************************************************
********************************************************************************
************ Credoc

*-- latest version of the original data file (on 08/09/2022): base78_20.dta


use "base78_20.dta", replace

gen year = 1977 + anneefuz
keep effortpp pond year
recode effortpp (4 = .)


save "CREDOC79_20", replace




********************************************************************************
********************************************************************************
********************************************************************************
************ DREES

*-- latest version of the original data file (on 08/09/2022): barometre_2000_2018_diff.dta


use "barometre_2000_2018_diff.dta", replace

rename annee year
keep ps1_1 ps1_2 ps1_3 ps1_4 poid year 
*-- wording of question changed in 2016
keep if year < 2016

save "DREES00_15", replace





********************************************************************************
********************************************************************************
********************************************************************************
************ BSAS83_17



******* the code below follows the folder structure, as provided by UK data service
** given the number of files, I recommend creating a separate folder

global pathBSAS = ".../rep_data/BSAS" 
cd "$pathBSAS"


*******************
****For 1983-1991: one merge file availabe 
********************


insheet using "UKDA-1983-1991-tab/tab/2955.tab", clear 

/* in some version of the data, the variable year is missing, you can use this code to create it 
** year

gen year = .

local i=83
while `i' <=91 {
replace year = 19`i' if study == "GB`i'"

local i = `i'+1
}
*/

** variables of interest + weights
destring morewelf welfhelp welffeet sochelp unempjob dolefidl dole wtf, replace


** income variable and household size  
destring incm8385 incm8687 incm89 incm90 incm91, gen(incm8385_n incm8687_n incm89_n incm90_n incm91_n)

gen hhincome_n = .
replace hhincome_n = incm8385_n
replace hhincome_n = incm8687_n if hhincome_n == .
replace hhincome_n = incm89_n if hhincome_n == .
replace hhincome_n = incm90_n if hhincome_n == .
replace hhincome_n = incm91_n if hhincome_n == .

*** need to compute household for 1983 and 1984

destring p11rel p12rel p13rel, replace
gen household_83 = .
replace household_83 = 1 if p2rel == 0  
replace household_83 = 2 if p3rel == 0 & p2rel !=0
replace household_83 = 3 if p4rel == 0 & p3rel !=0
replace household_83 = 4 if p5rel == 0 & p4rel !=0
replace household_83 = 5 if p6rel == 0 & p5rel !=0
replace household_83 = 6 if p7rel == 0 & p6rel !=0
replace household_83 = 7 if p8rel == 0 & p7rel !=0
replace household_83 = 8 if p9rel == 0 & p8rel !=0
replace household_83 = 9 if p10rel == 0 & p9rel !=0
replace household_83 = 10 if p11rel == 0 & p10rel !=0
replace household_83 = 11 if p12rel == 0 & p11rel !=0
replace household_83 = 12 if p13rel == 0 & p12rel !=0
replace household_83 = . if study != "GB83"

destring househld, replace
replace househld = household_83 if study == "GB83"

gen household_84 = .
replace household_84 = 1 if p2rel == 0  
replace household_84 = 2 if p3rel == 0 & p2rel !=0
replace household_84 = 3 if p4rel == 0 & p3rel !=0
replace household_84 = 4 if p5rel == 0 & p4rel !=0
replace household_84 = 5 if p6rel == 0 & p5rel !=0
replace household_84 = 6 if p7rel == 0 & p6rel !=0
replace household_84 = 7 if p8rel == 0 & p7rel !=0
replace household_84 = 8 if p9rel == 0 & p8rel !=0
replace household_84 = 9 if p10rel == 0 & p9rel !=0
replace household_84 = 10 if p11rel == 0 & p10rel !=0
replace household_84 = 11 if p12rel == 0 & p11rel !=0
replace household_84 = 12 if p13rel == 0 & p12rel !=0
replace household_84 = . if study != "GB84"

replace househld = household_84 if study == "GB84"

keep morewelf welfhelp welffeet sochelp unempjob dolefidl dole wtf hhincome_n househld year 


cd "$pathBSAS"
save "bsa1983_1991.dta", replace


*****************************
****For 1993 - 2009
************************


local i=1993
while `i' <=1996 {

cd "$pathBSAS"

insheet using "UKDA-`i'-tab/tab/bsa`i'.tab", clear 


capture drop year
gen year = `i'

gen hhincome_n = hhincome

keep morewelf welfhelp welffeet sochelp unempjob dolefidl dole wtf hhincome_n househld serial year
 
cd "$pathBSAS"
save "bsa`i'.dta", replace

clear all 

local i = `i'+1
}

** 1997 does not include "morewelf welfhelp welffeet sochelp unempjob dolefidl dole

local i=1998
while `i' <=2009 {

cd "$pathBSAS"

insheet using "UKDA-`i'-tab/tab/bsa`i'.tab", clear 

capture drop year
gen year = `i'

gen hhincome_n = hhincome

keep morewelf welfhelp welffeet sochelp unempjob dolefidl dole wtf hhincome_n househld serial year
 
cd "$pathBSAS"
save "bsa`i'.dta", replace

clear all 

local i = `i'+1
}



*****************************
****For 2010-2017
************************



local i=10
while `i' <=17 {
	

cd "$pathBSAS"

cd "UKDA-20`i'-tab/BSAS 20`i'"

use bsa`i'.dta, replace

gen year = 20`i'

capture rename Serial serial
capture rename SSerial serial
capture rename Sserial serial
capture rename WtFactor wtfactor
capture rename HHincome hhincome_n
capture rename Hhincome hhincome_n
capture rename Hhincome hhincome_n
capture rename HhIncD hhincome_n
capture rename HHIncD hhincome_n
capture rename Househld househld
capture rename househlde househld
capture rename househlde househld

capture rename MoreWelf morewelf
capture rename WelfFeet welffeet
capture rename WelfHelp welfhelp
capture rename SocHelp sochelp
capture rename UnempJob unempjob
capture rename DoleFidl dolefidl
capture rename Dole dole 

keep year morewelf welfhelp welffeet sochelp unempjob dolefidl dole wtf hhincome_n househld serial year
 
cd "$pathBSAS"
save "bsa20`i'.dta", replace

local i = `i'+1

}
*/

*********************
** MERGE 
***


clear all
cd "$pathBSAS"
use "bsa1983_1991.dta", replace 

local i=1993
while `i' <=1996 {
	
append using bsa`i'.dta	

local i = `i'+1

}

local i=1998
while `i' <=2017 {
	
append using bsa`i'.dta	

local i = `i'+1

}


cd "$pathData"
save "bsa1983_2017.dta", replace 





********************************************************************************
********************************************************************************
************ ISSP_RofG85_16.dta

* Start with the 1985-2006 cumulative datafile
use "ZA4747.dta", replace

gen year = 1985 if V4 == 1985
replace year = 1990 if V4 == 1990
replace year = 1996 if V4 == 1996
replace year = 2006 if V4 == 2006

gen cntry = "Great Britain" if V6 == 826
drop if cntry == ""

* V55 : government living standards of the unemployed
* V56 : government responsability to reduce diff Rich Poor

rename V55 UE
rename V56 red


rename V3 id

keep year cntry UE red weight id
save "RofG1_4G.dta", replace

* now turn to the 2016 wave 

use "ZA6900_v2-0-0", replace 

gen year = 2016

gen cntry = "Great Britain" if country == 826
drop if cntry == ""

* v26 : government living standards of the unemployed
* v27 : government responsability to reduce diff Rich Poor

rename v26 UE
rename v27 red

rename CASEID id
rename WEIGHT weight 


keep year cntry UE red weight id
save "RofG5G.dta", replace

append using "RofG1_4G.dta"
save "ISSP_RofG85_16.dta", replace




